package com.xing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xing.DTO.SysNotificationDTO;
import com.xing.Utils.Result;
import com.xing.config.JSONUtils;
import com.xing.dao.SysNotificationDao;
import com.xing.dao.SysUserDao;
import com.xing.entity.SysNotificationEntity;
import com.xing.service.SysNotificationService;
import com.xing.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author <a href="https://gitee.com/a-tom-is-cry">Xing</a>
 * @Date 2025/3/19 21:07
 * @Description
 */
@Service
public class SysNotificationServiceImpl extends ServiceImpl<SysNotificationDao, SysNotificationEntity>
        implements SysNotificationService {

    @Autowired
    private SysUserDao userDao;

    public QueryWrapper<SysNotificationEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<SysNotificationEntity> wrapper = new QueryWrapper<>();
        String title = (String) params.get("title");
        wrapper.like(!StringUtils.isEmpty(title), "title", title);
        String receivedId = (String) params.get("receivedId");
        wrapper.eq(!StringUtils.isEmpty(receivedId), "received_id", receivedId);
        String delFlag = (String) params.get("delFlag");
        wrapper.like(!StringUtils.isEmpty(delFlag), "del_flag", delFlag);
        String createDate = (String) params.get("createDate");
        wrapper.ge(!StringUtils.isEmpty(createDate), "create_date", createDate);
        wrapper.orderByDesc("create_date");
        return wrapper;
    }


    @Override
    public Result Page(Map<String, Object> params) {
        QueryWrapper<SysNotificationEntity> queryWrapper = getQueryWrapper(params);
        String page1 = (String) params.get("currentPage");
        String size = (String) params.get("pageSize");
        Page<SysNotificationEntity> page = new Page<>(Integer.parseInt(page1), Integer.parseInt(size));
        Page<SysNotificationEntity> pageInfo = super.page(page, queryWrapper);
        List<SysNotificationEntity> liveDTOS = JSONUtils.listParse(pageInfo.getRecords(), SysNotificationEntity.class);
        return new Result().ok(new PageData<>(liveDTOS, pageInfo.getTotal()));
    }

    @Override
    public Result SendNotification(SysNotificationDTO dto) {
        if (dto.getCreateDate() == null) {
            dto.setCreateDate(new Date());
        }
        boolean save = save(JSONUtils.objParse(dto, SysNotificationEntity.class));
        if (save) {
            return new Result().ok();
        }
        return new Result().err(501);
    }

    @Override
    public Result sendSystemNotification(SysNotificationDTO dto) {
        try {
            List<Long> allUserId = userDao.getAllUserId();
            for (Long aLong : allUserId) {
                dto.setReceivedId(aLong);
                SendNotification(dto);
            }
            return new Result().ok();
        } catch (Exception e) {
            return new Result().err(502);
        }
    }

    @Override
    public Result handleNotification(String id) {
        UpdateWrapper<SysNotificationEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("del_flag", 1)
                .eq("id", id);
        boolean update = update(wrapper);
        if (update) {
            return new Result().ok();
        }
        return new Result().err(501);
    }


}
